/*******************************************************************************
 * Copyright (c) 2007 University of Oslo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University of Oslo - initial API and implementation
 *******************************************************************************/

package com.cea.papyrus.diagram.sequence.sedi.model.command;

import static com.cea.papyrus.diagram.sequence.sedi.model.util.ModelUtil.createUniqueName;
import static com.cea.papyrus.diagram.sequence.sedi.model.util.ModelUtil.getOwnedElements;

import org.eclipse.gef.commands.Command;
import org.eclipse.uml2.uml.InteractionConstraint;
import org.eclipse.uml2.uml.InteractionOperand;
import org.eclipse.uml2.uml.OpaqueExpression;
import org.eclipse.uml2.uml.UMLFactory;

import com.cea.papyrus.diagram.sequence.sedi.model.util.OpaqueExpressionUtil;

/**
 * Command to create instances of InteractionConstraints.
 * 
 * <p>
 * The following attributes must be set for the command to complete
 * successfully:
 * </p>
 * <ul>
 * <li>Operand
 * </ul>
 * 
 * <p>
 * The following attributes can be read to access the result of the command:
 * </p>
 * <ul>
 * <li>Constraint
 * </ul>
 * 
 * @author Andreas Limyr
 */
public class CreateGuardCommand extends Command {

	private InteractionOperand operand;
	private InteractionConstraint constraint;

	public void setOperand(final InteractionOperand operand) {
		this.operand = operand;
	}

	public InteractionConstraint getConstraint() {
		return constraint;
	}

	@Override
	public void execute() {
		OpaqueExpression valueSpec = UMLFactory.eINSTANCE
				.createOpaqueExpression();
		valueSpec.setName("ValueSpecification");
		OpaqueExpressionUtil.addBody(valueSpec, "ExpressionBody");

		constraint = UMLFactory.eINSTANCE.createInteractionConstraint();
		constraint.setName(createUniqueName(getOwnedElements(operand),
				"Constraint_"));
		constraint.setSpecification(valueSpec);

		operand.setGuard(constraint);
	}

	@Override
	public void undo() {
		operand.setGuard(null);
	}

	@Override
	public void redo() {
		operand.setGuard(constraint);
	}

	@Override
	public void dispose() {
		constraint = null;
		operand = null;
	}
}
